package com.zzsong.lambda.pratice;


import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 需求描述：电商订单数据处理，根据下面的list1和list2各10个订单
 * 1、统计出同时被两个人购买的商品列表(交集)
 * 2、统计出两个人购买商品的差集
 * 3、统计出全部被购买商品的去重并集
 * 4、统计两个人的分别购买订单的平均价格
 */
public class Main {

    public static void main(String[] args) throws Exception {
        //总价 35
        List<VideoOrder> videoOrders1 = Arrays.asList(
                new VideoOrder("20190242812", "springboot教程", 3),
                new VideoOrder("20194350812", "微服务SpringCloud", 5),
                new VideoOrder("20190814232", "Redis教程", 9),
                new VideoOrder("20190523812", "网页开发教程", 9),
                new VideoOrder("201932324", "百万并发实战Netty", 9));
        //总价 54
        List<VideoOrder> videoOrders2 = Arrays.asList(
                new VideoOrder("2019024285312", "springboot教程", 3),
                new VideoOrder("2019081453232", "Redis教程", 9),
                new VideoOrder("20190522338312", "网页开发教程", 9),
                new VideoOrder("2019435230812", "Jmeter压力测试", 5),
                new VideoOrder("2019323542411", "Git+Jenkins持续集成", 7),
                new VideoOrder("2019323542424", "Idea全套教程", 21));
        //两个订单平均价格
        double videoOrderAvg1 = videoOrders1.stream().collect(Collectors.averagingInt(VideoOrder::getMoney));
        System.out.println("订单列表1平均价格="+videoOrderAvg1);
        double videoOrderAvg2 = videoOrders2.stream().collect(Collectors.averagingInt(VideoOrder::getMoney));
        System.out.println("订单列表2平均价格="+videoOrderAvg2);
        //订单总价
        int totalMoney1 = videoOrders1.stream().mapToInt(VideoOrder::getMoney).sum();
        System.out.println("订单列表1总价="+totalMoney1);
        int totalMoney2 = videoOrders2.stream().mapToInt(VideoOrder::getMoney).sum();
        System.out.println("订单列表2总价="+totalMoney2);
        //交集
        List<VideoOrder> intersectionList = videoOrders1.stream().filter(videoOrders2::contains).collect(Collectors.toList());
        System.out.println(intersectionList);
        //差集1
        List<VideoOrder> diffList1 =  videoOrders1.stream().filter(obj->!videoOrders2.contains(obj)).collect(Collectors.toList());
        System.out.println("差集1="+diffList1);
        List<VideoOrder> diffList2 =  videoOrders2.stream().filter(obj->!videoOrders1.contains(obj)).collect(Collectors.toList());
        System.out.println("差集2="+diffList2);
        //并集
        List<VideoOrder> allVideoOrder = videoOrders1.parallelStream().collect(Collectors.toList());
        allVideoOrder.addAll(videoOrders2);
        System.out.println("并集="+allVideoOrder);
        //去重并集
        List<VideoOrder> allVideoOrderDistinct = allVideoOrder.parallelStream().distinct().collect(Collectors.toList());
        System.out.println("去重并集="+allVideoOrderDistinct);
    }
}

